package org.example.test1.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.test1.pojo.JwtUtils;

import java.io.IOException;
import java.util.regex.Pattern;

//@WebFilter(urlPatterns = "/*")
public class Myfiler implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        String url = httpServletRequest.getRequestURL().toString();
        if(url.contains("tg")){
            filterChain.doFilter(servletRequest,servletResponse);
        }
        else {
            String jwt = httpServletRequest.getHeader("token");
            try {
                JwtUtils.parseJWT(jwt);
            } catch (Exception e) {
                System.out.println("令牌错误");
                return;
            }
            filterChain.doFilter(servletRequest,servletResponse);
        }

    }
}
